package pucp.carpooling;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import pucp.carpooling.adapter.ListViewAdapterConductor;
import pucp.carpooling.controller.JSONParserObject;
import com.actionbarsherlock.app.SherlockFragment;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import com.costum.android.widget.LoadMoreListView;
import com.costum.android.widget.PullAndLoadListView;
import com.costum.android.widget.LoadMoreListView.OnLoadMoreListener;
import com.costum.android.widget.PullToRefreshListView.OnRefreshListener;

import pucp.carpooling.R;
import android.widget.ListView;

public class FragmentTab_aviso_conductor extends SherlockFragment {
	private static final String url = "http://subete.inf.pucp.edu.pe/ServiciosMobile/obtener_avisos_conductor";
	String[] idavisos,idtrips,conductor, direccion, fecha, libres, precio ,idconductores;
	private static final String SUCCESS = "Success";
	private static final String TAG_AvisosConductor = "AvisosConductor";
	private static final String TAG_FirstName = "FirstName";
	private static final String TAG_LastName = "LastName";
	private static final String TAG_TripDateTime = "TripDateTime";
	private static final String TAG_DestinyName = "DestinyName";
	int[] flag;
	ListView list;
	ListViewAdapterConductor adapter;
	JSONParserObject jsonParser = new JSONParserObject();
	JSONArray avisos = null;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragmenttab_aviso_conductor, container,
				false);
		
		
		new getAvisosConductor().execute();

		list = (ListView) rootView.findViewById(R.id.listview);
		list.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
			
				Intent i = new Intent(getActivity(), SingleItemViewConductor.class);				
				String coords = null;
			
				try {
					//coords =avisos.getJSONObject(position-1).getString("Detail");
					// Posterior a agregar el pull, ahora tengo que restar uno
					coords =avisos.getJSONObject(position).getString("Detail");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				i.putExtra("coordenadas", coords);								
				i.putExtra("conductor", conductor);
				i.putExtra("direccion", direccion);
				i.putExtra("fecha", fecha);
				i.putExtra("precio", precio);
				i.putExtra("libres", libres);
				i.putExtra("flag", flag);
				i.putExtra("idavisos", idavisos);
				i.putExtra("idtrips", idtrips);
				i.putExtra("position", position);				
				i.putExtra("idconductores", idconductores);			
				
				startActivity(i);
			}

		});
		

		
		
		return rootView;
	}
	private class getAvisosConductor extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {

		}

		@Override
		protected Void doInBackground(Void... arg0) {
			try {

				JSONParserObject sh = new JSONParserObject();

			
				String jsonStr = sh.makeServiceCall(url, JSONParserObject.GET);
				Log.d("Response: ", "> " + jsonStr);
				if (jsonStr != null) {
					try {
						JSONObject jsonObj = new JSONObject(jsonStr);
						if (jsonObj.getString(SUCCESS).equals("Yes")) {
							avisos = jsonObj.getJSONArray(TAG_AvisosConductor);
							
							conductor = new String[avisos.length()];
							direccion = new String[avisos.length()];
							libres =  new String[avisos.length()];
							precio =  new String[avisos.length()];
							idtrips = new String[avisos.length()];
							fecha = new String[avisos.length()];
							idavisos = new String[avisos.length()];
							flag = new int[avisos.length()];
							
							//adding begin
							idconductores = new String[avisos.length()];
							//adding end
							
							for (int i = 0; i < avisos.length(); i++) {
								JSONObject c = avisos.getJSONObject(i);
								conductor[i] = c.getString(TAG_FirstName) + " "
										+ c.getString(TAG_LastName);
								direccion[i] = " " +c.getString(TAG_DestinyName);
								idavisos[i] = c.getString("idAdvice");		
								fecha[i] = c.getString(TAG_TripDateTime);
								precio[i] = c.getString("PricePerPerson");
								libres[i] = c.getString("AvailableSeats");
								idtrips[i] =  c.getString("idTrip");
								
								//adding begin
								idconductores[i] = c.getString("idUser");
								//adding end
								
								if (c.getString("Gender").equals("F"))
								flag[i] = R.drawable.female80x80;
								else								
								  flag[i] = R.drawable.male80x80;
							}
						} else {
							// no hay viajes pe :O
						}
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}

			} catch (Exception e) {

			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
		

			if (conductor != null) {
				adapter = new ListViewAdapterConductor(getActivity(), conductor,
						direccion, fecha, libres, precio, flag); 
				
				list.setAdapter(adapter);
			}
			
//			((PullAndLoadListView) list).onLoadMoreComplete();

			
			super.onPostExecute(result);		
		}
		
		@Override
		protected void onCancelled() {
//			((PullAndLoadListView) list).onLoadMoreComplete();
		}
	}

}
